package com.novel.authorization.staff.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.novel.authorization.staff.entity.NfUser;
import com.novel.authorization.staff.mapper.NfUserMapper;
import com.novel.authorization.staff.service.NfGroupService;
import com.novel.authorization.staff.service.NfUserExpandService;
import com.novel.authorization.staff.service.NfUserService;
import com.novel.authorization.staff.vo.UserInfoVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户
 *
 * @author 樊温军
 * @date 2025/3/18 上午10:32
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class NfUserServiceImpl extends ServiceImpl<NfUserMapper, NfUser> implements NfUserService {

    private final NfUserExpandService nfUserExpandService;
    private final NfGroupService nfGroupService;

    @Override
    public List<NfUser> getNfUserList(NfUser nfUser) {
        return baseMapper.getList(nfUser);
    }

    @Override
    public IPage<NfUser> getPage(Page<NfUser> page, NfUser nfUser) {
        return baseMapper.getPage(page, nfUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long saveOneNfUser(NfUser nfUser) {
        this.saveOrUpdate(nfUser);
        return nfUser.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delById(Long id) {
        baseMapper.delete(Wrappers.<NfUser>query().lambda().in(NfUser::getId, id));
    }

    @Override
    public UserInfoVO getUserInfo(NfUser user) {
        UserInfoVO userInfoVO = new UserInfoVO();
        userInfoVO.setUser(user);
        userInfoVO.setUserExpand(nfUserExpandService.getById(user.getId()));
        userInfoVO.setGroups(nfGroupService.getGroupList(user.getId()));
        return userInfoVO;
    }

    @Override
    public NfUser queryById(Long id) {
        return baseMapper.selectById(id);
    }

}
